package com.cspg.ssss.service;

import com.cspg.ssss.domain.GradeApproveEntity;
import com.cspg.ssss.repository.GradeApproveEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApproveService {

    @Autowired
    GradeApproveEntityMapper gradeApproveEntityMapper;

    public Map<String,GradeApproveEntity> getApprovedList(String pid){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("problem_id",pid);
        List<GradeApproveEntity> retls = gradeApproveEntityMapper.selectApprove(condition);

        HashMap<String,GradeApproveEntity> retMap = new HashMap<String,GradeApproveEntity>();
        retMap.put("check",null);
        retMap.put("approve",null);
        retMap.put("approve_review",null);
        retMap.put("review",null);
        if(retls != null && retls.size() > 0){
            for(int i=0;i<retls.size();i++){
                GradeApproveEntity en = retls.get(i);
                if(en.getApprove_type() == 0){
                    retMap.put("check",en);
                }
                if(en.getApprove_type() == 1){
                    retMap.put("approve",en);
                }
                if(en.getApprove_type() == 2){
                    retMap.put("review",en);
                }
                if(en.getApprove_type() == 5){
                    retMap.put("approve_review",en);
                }
            }
        }

        return retMap;
    }

    public Map<String,GradeApproveEntity> getScoreAddApprovedList(String pid){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("problem_id",pid);
        List<GradeApproveEntity> retls = gradeApproveEntityMapper.selectApprove(condition);

        HashMap<String,GradeApproveEntity> retMap = new HashMap<String,GradeApproveEntity>();
        retMap.put("op_check",null);
        retMap.put("op_review",null);
        if(retls != null && retls.size() > 0){
            for(int i=0;i<retls.size();i++){
                GradeApproveEntity en = retls.get(i);
                if(en.getApprove_type() == 3){
                    retMap.put("op_check",en);
                }
                if(en.getApprove_type() == 4){
                    retMap.put("op_review",en);
                }
            }
        }

        return retMap;
    }

    public int updateApprove(GradeApproveEntity en){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("problem_id",en.getProblem_id());
        condition.put("approve_type",en.getApprove_type());
        List<GradeApproveEntity> retls = gradeApproveEntityMapper.selectApprove(condition);

        int ret = 0;
        if(retls != null && retls.size() > 0){
            en.setId(retls.get(0).getId());
            ret = gradeApproveEntityMapper.updateApprove(en);
        }else {
            ret = gradeApproveEntityMapper.insertApprove(en);
        }

        return ret;
    }
}
